<template>
  <div class="goodinfo-container">
    <transition
      @before-enter="beforeEnter"
      @enter="enter"
      @after-enter="afterEnter"
    >
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :images="img" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买区 -->
    <div class="mui-card">
      <div class="mui-card-header">商品的名称标题{{ goodinfo[0].title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="prize">
            市场价：<del>{{ goodinfo[0].market_price }}</del> 销售价：{{
              goodinfo[0].sell_price
            }}<span class="now_prize"></span>
          </p>
          <p>
            购买数量：<num
              @getcount="getSelectedCound"
              :max="goodinfo[0].stock_quantity"
            ></num>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar"
              >加入购物车</mt-button
            >
          </p>
        </div>
      </div>
    </div>
    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{ goodinfo[0].goods_no }}</p>
          <p>库存情况：{{ goodinfo[0].stock_quantity }}</p>
          <p>
            上架时间：{{
              goodinfo[0].add_time | dateformat("YYYY-MM-DD HH:mm:ss")
            }}
          </p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button
          type="primary"
          size="large"
          plain
          @click="goodsinfo(goodinfo[0].id)"
          >图文介绍</mt-button
        >
        <mt-button
          type="danger"
          size="large"
          plain
          @click="comm(goodinfo[0].id)"
          >商品评论</mt-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import { sulvetu, goodsprize } from "../../../static/api.js";
import swiper from "../../components/swiper";
import num from "../../components/numbernum.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      img: [{}],
      goodinfo: [{}],
      ballFlag: false,
      selectedCound:1
    };
  },
  methods: {
    goodsinfo(id) {
      this.$router.push("/home/shop/shopdetail/" + id);
    },
    comm(id) {
      this.$router.push("/home/shop/shopcom/" + id);
    },
    addToShopCar() {
      //添加到购物车
      this.ballFlag = !this.ballFlag;
      var goods={id:this.id,count:this.selectedCound,prize:this.goodinfo[0].sell_price,selector:true}
    this.$store.commit("addToCar",goods)
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },

    enter(el, done) {
      el.offsetWidth;
      //小球优化:
      //getBoundingClientRect()

      const ballPosition = this.$refs.ball.getBoundingClientRect();
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();
      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px,${yDist}px)`;
      el.style.transition = "all .5s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter() {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCound(count) {
      this.selectedCound = count;
      console.log("父拿到的是" + this.selectedCound);
    },
  },

  async created() {
    let a = await sulvetu(this.id);
    a.forEach((item) => {
      item.image_src = item.src;
    });
    this.img = a;
    this.goodinfo = await goodsprize(this.id);
    console.log(this.goodinfo);
  },
  components: {
    swiper,
    num,
  },
};
</script>
<style lang="scss" scoped>
.goodinfo-container {
  background-color: #eee;
  overflow: hidden;
  margin-bottom: 51px;
  text-align: left;
}
.now_prize {
  color: red;
  font-size: 16px;
  font-weight: bold;
}
.mui-card-footer {
  display: block;
  button {
    margin: 10px 0;
  }
}
.ball {
  width: 15px;
  height: 15px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
  top: 390px;
  left: 150px;
  z-index: 99;
}
</style>
